from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.chrome.service import Service

import sys
sys.path.append(r'..')
from web_base.base_page import BasePage


class DiscuzPage(BasePage):
    url='http://192.168.92.131/upload/'
    #登录定位
    username_loc=(By.ID,'ls_username')
    password_loc=(By.ID,'ls_password')
    login_btn_loc=(By.CLASS_NAME,'pn.vm')

    def __init__(self,dirver):
        super().__init__(dirver)
        self.get_url(self.url)

    def login(self,username,password):
        self.ele_clear(self.username_loc)
        self.input_ele(self.username_loc,username)
        self.ele_clear(self.password_loc)
        self.input_ele(self.password_loc,password)
        self.ele_click(self.login_btn_loc)
        sleep(3)
    #退出定位
    logout_btn_loc=(By.LINK_TEXT,'退出')
    def logout(self):
        self.ele_click(self.logout_btn_loc)
        sleep(3)
    #注册定位
    register_loc=(By.LINK_TEXT, '立即注册')
    newname_loc=(By.ID,'IouQbW')
    pwd_loc=(By.ID,'cWFNZj')
    pwds_loc=(By.ID,'d8wZnh')
    email_loc=(By.ID,'tu7mNL')
    register_btn_loc=(By.ID,'registerformsubmit')
    def register(self,newname,pwd,pwds,email):
        self.ele_click(self.register_loc)
        self.ele_clear(self.newname_loc)
        self.input_ele(self.newname_loc,newname)
        self.ele_clear(self.pwd_loc)
        self.input_ele(self.pwd_loc,pwd)
        self.ele_clear(self.pwds_loc)
        self.input_ele(self.pwds_loc, pwds)
        self.ele_clear(self.email_loc)
        self.input_ele(self.email_loc,email)
        self.ele_click(self.register_btn_loc)
        sleep(6)
    #查看帖子
    read_loc=(By.XPATH,'//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    sleep(2)
    read_click_loc=(By.CLASS_NAME,'s.xst')
    def read(self,username='admin',password='123456'):
        self.login(username,password)
        self.ele_click(self.read_loc)
        sleep(2)
        self.ele_click(self.read_click_loc)
        sleep(2)

    #发贴
    post_click_loc=(By.XPATH,'//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    post_click=(By.ID,'newspecial')
    post_title_loc=(By.ID,'subject')
    post_body_loc=(By.XPATH,'/html/body')
    post_cli_loc=(By.ID,'postsubmit')
    def post(self,subject, message,username='admin',password='123456'):
        self.login(username,password)
        self.ele_click(self.post_click_loc)
        self.ele_click(self.post_click)
        sleep(1)
        self.input_ele(self.post_title_loc,subject)
        sleep(1)
        self.driver.switch_to.frame('e_iframe')
        sleep(1)
        self.input_ele(self.post_body_loc,message)
        sleep(1)
        self.driver.switch_to.default_content()
        self.ele_click(self.post_cli_loc)
        sleep(1)

    #回复帖子
    reply_click_loc=(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a')
    reply_cli_loc=(By.CLASS_NAME, 's.xst')
    reply_loc=(By.ID, 'post_reply')
    replys_loc=(By.ID, 'postmessage')
    reply_tit_loc=(By.ID, 'postsubmit')

    def reply(self,text, username='admin', password='123456'):
        self.login(username,password)
        self.ele_click(self.reply_click_loc)
        sleep(1)
        self.ele_click(self.reply_cli_loc)
        sleep(1)
        self.ele_click(self.reply_loc)
        sleep(1)
        self.input_ele(self.replys_loc,text)
        sleep(1)
        self.ele_click(self.reply_tit_loc)
        sleep(3)






if __name__ == '__main__':
    username='admin'
    passward='123456'
    newname='wjc03'
    pwd="123456"
    pwds="123456"
    email='wjc03@qq.com'

    subject='aohfdon'
    message='关于源总喜欢粉色这件事！'
    text='66666'
    chrome_location = r'D:\测试工具\Win_x64_1135105_chrome-win\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location
    driver_location = r'D:\测试工具\pythan\chromedriver.exe'
    service = Service(driver_location)
    driver = webdriver.Chrome(options=options, service=service)

    discuz_page=DiscuzPage(driver)
    # discuz_page.assert(username,passward)
    # discuz_page.register(newname,pwd,pwds,email)
    # discuz_page.logout()
    # discuz_page.read()
    discuz_page.post(subject,message)
    # discuz_page.reply(text)
    driver.quit()